<?php
include_once(dirname(__FILE__)."/../inc/common.php");
$method = isset($_GET["method"])?$_GET["method"]:"";

if($method=="listQry"){
	$accountType = isset($_POST["accountType"])?$_POST["accountType"]:null;
	$showInvisible = isset($_GET["showInvisible"]) && ($_GET["showInvisible"]==1)?1:0;
	$tallyAccountClass = new tally_account();
	$accountList = $tallyAccountClass->findAccountList($userId, $showInvisible, $accountType);
	?>
	 <!--begin:数据列表-------------------------------------------------------------------------------------------------->
	    <form name="accountSchForm" id="accountSchForm" method="post">
	    <table id="listtb" class="listtable" cellpadding="0" cellspacing="1">
	      <thead>
		      <tr>
	            <th width="5%">状态</th>
		        <th width="18%">名称</th>
		        <th width="10%">类型</th>
		        <th width="14%">当前余额</th>
		        <th width="14%">创建日期</th>
		        <th width="20%">说明</th>
		        <th width="5%">顺序</th>
		      </tr>
	      </thead>
	      <tbody>
			<?php
			foreach($accountList as $account){
			?>
	      <tr>
		            <td>
		            	<?php   if($account["isShow"]){
		            				if($account["isDefault"]){
		            	?>
		            	<img style="padding-left: 1px;" src="../images/common/run.gif"/>
		            	<?php
		            				}
		            			}else{ ?>
		            	<img style="padding-left: 1px;" src="../images/common/stop.gif"/>
		            	<?php   } ?>
						<input type="hidden" name="subjectId" id="subjectId" value="<?php echo $account["id"]?>">
		            </td>
			        <td class="to_left">
		                 <?php echo $account["name"]?>
			        </td>
	        		<td><?php echo $accountTypeList[$account["accountType"]]?> </td>

			        <td class="number" style="color:;"><?php echo $account["currentValue"]?></td>


                    <td><?php echo $account["createTime"]?></td>
	                <td  class="char"><?php echo $account["description"]?></td>
			        <td>

                           <label for="sort<?php echo $account["id"]?>" style="display:none">顺序</label>
				           <input type="text"  id="sort<?php echo $account["id"]?>" notnull="true" datatype="integer" id="<?php echo $account["id"]?>" name="<?php echo $account["id"]?>" value="<?php echo $account["orderIndex"]?>" maxlength="5" style="width:30px;text-align:center;border:0px;border-bottom:1px #000000 solid;"/>

                        <div class="oper_bar">
						    <span> <a  title="修改" href="javascript:fGoto();" onclick="Account.goEdit('<?php echo $account["id"]?>','0');">编辑</a></span>
							<span> <a  title="删除" href="javascript:fGoto();" onclick="Account.onDelete('<?php echo $account["id"]?>');">删除</a></span>
			                <?php if(!$account["isDefault"]){ ?>
			                <span><a  title="<?php echo $account["isShow"]?"隐藏":"恢复"?>"  href="javascript:fGoto();"
				                onclick="Account.alterHide('<?php echo $account["id"]?>','<?php echo $account["isDefault"]?>','<?php echo $account["isShow"]?0:1?>');">
				                <?php echo $account["isShow"]?"隐藏":"恢复"?></a></span>
			                <span><a  title="<?php echo $account["isDefault"]?"取消":"设为"?>默认"  href="javascript:fGoto();"
			                	onclick="Account.alterDefault('<?php echo $account["id"]?>','0','<?php echo $account["isDefault"]?>');">
			                	<?php echo $account["isDefault"]?"取消":"设为"?>默认</a></span>
			                <?php } ?>
                      </div>
			        </td>
	       </tr>
	       <?php
			}
			?>

	       </tbody>
        <tfoot>
		</tfoot>
	    </table>
        </form>
           <?php
			if(!$showInvisible){
			?>
           <a  id="Btn3"  title="隐藏/恢复"  href="javascript:fGoto();" onclick="Account.search('1');"><span>--显示隐藏的账户--</span></a>
	       <?php
			}else{
				?>
			<a  id="Btn3"  title="隐藏/恢复"  href="javascript:fGoto();" onclick="Account.search('0');"><span>--不显示隐藏的账户--</span></a>
				<?php
			}
			?>



	    <!--end:数据列表区------------------>
	<?php
}else if($method=="add"){
	?>
<div id="winwrapper" class="winwrapper">
<form id="accountAddForm" name="theForm"  method="post">
	<div class="content">
			<fieldset>
			<legend>基本信息</legend>
				 <table cellpadding="0" cellspacing="1" class="form_table">
					 <input name="accountType" id="accountType" type="hidden" value="0">
					 <input name="initAmount" id="initAmount" type="hidden" value="0">
                   <tr>
                      <td class="caption"><span class="em">*</span><label for="subjectName">账户名称:</label></td>
                      <td class="field"><input type="text" class="text m"  name="subjectName" notnull="true" id="subjectName"  maxlength="20" value=""/></td>
                   </tr>
                    <tr>
                      <td class="caption"><label for="remark">说明:</label></td>
                      <td class="field"><input type="text" class="text l" name="remark" id="remark"  maxlength="30" value=""/></td>
                   </tr>
                </table>

			</fieldset>


	        <div class="buttons">
                    <input id="saveBtn" type="button" class="button"  value="确定" onclick="Account.save(this);">
			        <input type="button" class="button"  value="取消" onclick="CM.close(this);return false;">
			</div>
	</div>
</form>
</div>
	<?php
}else if($method=="edit"){
	$oid = isset($_GET["oid"])?$_GET["oid"]:0;
	$tallyAccountClass = new tally_account();
	$account = $tallyAccountClass->findAccountByAccountId($userId, $oid);
	?>
<div id="winwrapper" class="winwrapper">
<form id="accountEditForm" name="accountEditForm"  method="post">
<input type="hidden" name="subjectId" id="subjectId" value="<?php echo $oid?>">
 <input  type="hidden" name="entryId" value="">
 <input type="hidden" name="voucherId" value="">
	<div class="content">
		    <fieldset>
				<legend>账户信息</legend>
					<table cellpadding="0" cellspacing="1" class="form_table">
						<tr>
							<td class="caption"><label for="">账户类型:</label></td>
							<td class="field">
                                <select class="m" name="accountType"  id="accountType" notnull="true" >
                                    <?php
                                    foreach($accountTypeList as $k=>$accountType){
                                        ?>
                                        <option value="<?php echo $k;?>" <?php echo $k==$account["accountType"]?"selected":""?>><?php echo $accountType;?></option>
                                        <?php
                                    }
                                    ?>
                                </select>
                            </td>
						</tr>
						<tr>
							<td class="caption"><label for="subjectName"><span class="em">*</span>账户名称:</label></td>
							<td class="field"><input type="text" class="text m" name="subjectName" notnull="true" id="subjectName"  maxlength="20" value="<?php echo $account["name"]?>"/></td>
						</tr>
						<tr>
							<td class="caption"><label for="remark">说明:</label></td>
							<td class="field"><input type="text" class="text l" name="remark" id="remark"  maxlength="30" value="<?php echo $account["description"]?>"/></td>
						</tr>
					</table>
			</fieldset>


	        <div class="buttons">
			        <input id="saveBtn" type="button" class="button"  value="确定" onclick="Account.update(this);">
			        <input type="button" class="button"  value="取消" onclick="CM.close(this);return false;">
			</div>
			<div class="tip_wrapper" style="display:none">
				<a class="colse_btn" onclick="Help.goClose();" href="#" align="absmiddle"><img src="../images/icons/icon_close.gif"/></a>
				<span class="title">提示:</span>
				<span class="tip_content"></span>
			</div>
	</div>
</form>
</div>
	<?php
}else if($method=="doAdd"){
	if(isset($_POST["accountType"]) && isset($_POST["subjectName"]) && $_POST["subjectName"]){
		try{
			$desc = isset($_POST["remark"])?$_POST["remark"]:"";
			$tallyAccountClass = new tally_account();
			$tallyAccountClass->createAccount(
				$userId, $_POST["accountType"],
				$_POST["subjectName"], $desc
			);
			$result = array(
				"returnCode" => 0,
				"returnMsg" => "新增成功",
			);
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"新增失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="doEdit"){
	if(isset($_POST["subjectId"]) && $_POST["subjectId"]
			&& isset($_POST["subjectName"]) && $_POST["subjectName"]){
		try{
			$desc = isset($_POST["remark"])?$_POST["remark"]:"";
			$tallyAccountClass = new tally_account();
			$account = $tallyAccountClass->findAccountByAccountId($userId, $_POST["subjectId"]);
			if($account){
                $account["name"] = $_POST["subjectName"];
                $account["accountType"] = $_POST["accountType"];
				$account["description"] = $_POST["remark"];
				$tallyAccountClass->updateAccountInfo($userId, $_POST["subjectId"], $account);
				$result = array(
					"returnCode" => 0,
					"returnMsg" => "修改成功",
				);
			}else{
				throw new accountException("此账户不存在");
			}
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"修改失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="alterHide"){
	if(isset($_GET["oid"]) && $_GET["oid"]){
		try{
			$tallyAccountClass = new tally_account();
			$account = $tallyAccountClass->findAccountByAccountId($userId, $_GET["oid"]);
			if($account){
				$account["isShow"] = $account["isShow"]?0:1;
				$tallyAccountClass->updateAccountInfo($userId, $_GET["oid"], $account);
				$result = array(
					"returnCode" => 0,
					"returnMsg" => "修改成功",
				);
			}else{
				throw new accountException("此账户不存在");
			}
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"修改失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="alterDefault"){
	if(isset($_GET["oid"]) && $_GET["oid"]){
		try{
			$tallyAccountClass = new tally_account();
			$account = $tallyAccountClass->findAccountByAccountId($userId, $_GET["oid"]);
			if($account){
				$tallyAccountClass->setDefaultAccount($userId, $_GET["oid"]);
				$result = array(
					"returnCode" => 0,
					"returnMsg" => "修改成功",
				);
			}else{
				throw new accountException("此账户不存在");
			}
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"修改失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="delete"){
	if(isset($_GET["oid"]) && $_GET["oid"]){
		try{
			$tallyAccountClass = new tally_account();
			$account = $tallyAccountClass->findAccountByAccountId($userId, $_GET["oid"]);
			if($account){
				$tallyAccountClass->removeAccount($userId, $_GET["oid"]);
				$result = array(
					"returnCode" => 0,
					"returnMsg" => "删除成功",
				);
			}else{
				throw new accountException("此账户不存在");
			}
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"删除失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="saveSort"){
	try{
		$tallyAccountClass = new tally_account();
		foreach($_POST as $accountId=>$orderIndex){
			$accountId+=0;
			$orderIndex+=0;
			if($accountId>0){
				$account = $tallyAccountClass->findAccountByAccountId($userId, $accountId);
				if($account){
					$account["orderIndex"] = $orderIndex;
					$tallyAccountClass->updateAccountInfo($userId, $accountId, $account);
				}
			}
		}
		$result = array(
			"returnCode" => 0,
			"returnMsg" => "保存顺序成功",
		);
	}catch(Exception $e){
		$result = array(
			"returnCode" => 2,
			"returnMsg" => $e->getMessage()?$e->getMessage():"保存顺序失败",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else{
?>
		<!-- begin:导航条 -->
		<div class="titlebar">
		   <h3>账户管理</h3>
		</div>
		<!-- end:导航条 -->

	<div class="tab_wrapper">
	  <div class="body">
		<!-- begin:工具条 -->
	    <div class="toolbar">
		    <p>
		  	  <a  id="addBtn" class="button" title="新增账户"  href="javascript:fGoto();" onclick="Account.goAdd();"><span>新增账户</span></a>
		  	  <a  id="sortBtn" class="button" title="保存排序"  href="javascript:fGoto();" onclick="Account.saveSort();"><span>保存顺序</span></a>
		    </p>
		</div>
		<!-- end:工具条 -->

		<!-- begin:搜索区 -->
		<form id="schForm" method="post">
		<div class="searchbar" id ="searchbar">
			<div class="base_condition">

				<table>
				<tr>
					<td>
						<label for="accountType">账户类型:</label>
						<SELECT name="accountType" class="SELECT" id="accountType" onChange="Account.pg.params='';Account.search();">
				    		<option value="">全部</option>
				    			<?php
				    				foreach($accountTypeList as $i=>$accountType){
				    			?>
				      			<option value="<?php echo $i?>"><?php echo $accountType?></option>
				    			<?php
				    				}
				    			?>

						</SELECT>
					</td>
				</tr>
				</table>
			</div>
		</div>
		</form>
		<!-- end:搜索区 -->

		<!-- begin:-------数据区 -------------------->
		<div id="dataView" class="data list_area">
			 <div  align="center"><img align="absmiddle" src='../images/common/loading1.gif'></div>
		</div>
		<!--end: ---------数据区------------------->
	  </div>
	</div>

<?php
}
?>
